package qldc.pm.tienan.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.tienan.NoSuchDoiTuongTienAnException;
import qldc.pm.tienan.model.DoiTuongTienAn;
import qldc.pm.tienan.model.impl.DoiTuongTienAnImpl;
import qldc.pm.tienan.model.impl.DoiTuongTienAnModelImpl;
import qldc.pm.tienan.service.persistence.DoiTuongTienAnPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the doi tuong tien an service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see DoiTuongTienAnPersistence
 * @see DoiTuongTienAnUtil
 * @generated
 */
public class DoiTuongTienAnPersistenceImpl extends BasePersistenceImpl<DoiTuongTienAn>
    implements DoiTuongTienAnPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link DoiTuongTienAnUtil} to access the doi tuong tien an persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = DoiTuongTienAnImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            DoiTuongTienAnImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            DoiTuongTienAnImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_FETCH_BY_SODINHDANHCANHAN = new FinderPath(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED,
            DoiTuongTienAnImpl.class, FINDER_CLASS_NAME_ENTITY,
            "fetchBySoDinhDanhCaNhan", new String[] { String.class.getName() },
            DoiTuongTienAnModelImpl.SODINHDANHCANHAN_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_SODINHDANHCANHAN = new FinderPath(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countBySoDinhDanhCaNhan", new String[] { String.class.getName() });
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1 =
        "doiTuongTienAn.soDinhDanhCaNhan IS NULL";
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2 =
        "doiTuongTienAn.soDinhDanhCaNhan = ?";
    private static final String _FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3 =
        "(doiTuongTienAn.soDinhDanhCaNhan IS NULL OR doiTuongTienAn.soDinhDanhCaNhan = '')";
    private static final String _SQL_SELECT_DOITUONGTIENAN = "SELECT doiTuongTienAn FROM DoiTuongTienAn doiTuongTienAn";
    private static final String _SQL_SELECT_DOITUONGTIENAN_WHERE = "SELECT doiTuongTienAn FROM DoiTuongTienAn doiTuongTienAn WHERE ";
    private static final String _SQL_COUNT_DOITUONGTIENAN = "SELECT COUNT(doiTuongTienAn) FROM DoiTuongTienAn doiTuongTienAn";
    private static final String _SQL_COUNT_DOITUONGTIENAN_WHERE = "SELECT COUNT(doiTuongTienAn) FROM DoiTuongTienAn doiTuongTienAn WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "doiTuongTienAn.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No DoiTuongTienAn exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No DoiTuongTienAn exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(DoiTuongTienAnPersistenceImpl.class);
    private static DoiTuongTienAn _nullDoiTuongTienAn = new DoiTuongTienAnImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<DoiTuongTienAn> toCacheModel() {
                return _nullDoiTuongTienAnCacheModel;
            }
        };

    private static CacheModel<DoiTuongTienAn> _nullDoiTuongTienAnCacheModel = new CacheModel<DoiTuongTienAn>() {
            @Override
            public DoiTuongTienAn toEntityModel() {
                return _nullDoiTuongTienAn;
            }
        };

    public DoiTuongTienAnPersistenceImpl() {
        setModelClass(DoiTuongTienAn.class);
    }

    /**
     * Returns the doi tuong tien an where soDinhDanhCaNhan = &#63; or throws a {@link qldc.pm.tienan.NoSuchDoiTuongTienAnException} if it could not be found.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the matching doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchDoiTuongTienAnException if a matching doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn findBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws NoSuchDoiTuongTienAnException, SystemException {
        DoiTuongTienAn doiTuongTienAn = fetchBySoDinhDanhCaNhan(soDinhDanhCaNhan);

        if (doiTuongTienAn == null) {
            StringBundler msg = new StringBundler(4);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("soDinhDanhCaNhan=");
            msg.append(soDinhDanhCaNhan);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isWarnEnabled()) {
                _log.warn(msg.toString());
            }

            throw new NoSuchDoiTuongTienAnException(msg.toString());
        }

        return doiTuongTienAn;
    }

    /**
     * Returns the doi tuong tien an where soDinhDanhCaNhan = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the matching doi tuong tien an, or <code>null</code> if a matching doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn fetchBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws SystemException {
        return fetchBySoDinhDanhCaNhan(soDinhDanhCaNhan, true);
    }

    /**
     * Returns the doi tuong tien an where soDinhDanhCaNhan = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @param retrieveFromCache whether to use the finder cache
     * @return the matching doi tuong tien an, or <code>null</code> if a matching doi tuong tien an could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn fetchBySoDinhDanhCaNhan(String soDinhDanhCaNhan,
        boolean retrieveFromCache) throws SystemException {
        Object[] finderArgs = new Object[] { soDinhDanhCaNhan };

        Object result = null;

        if (retrieveFromCache) {
            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    finderArgs, this);
        }

        if (result instanceof DoiTuongTienAn) {
            DoiTuongTienAn doiTuongTienAn = (DoiTuongTienAn) result;

            if (!Validator.equals(soDinhDanhCaNhan,
                        doiTuongTienAn.getSoDinhDanhCaNhan())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_SELECT_DOITUONGTIENAN_WHERE);

            boolean bindSoDinhDanhCaNhan = false;

            if (soDinhDanhCaNhan == null) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1);
            } else if (soDinhDanhCaNhan.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3);
            } else {
                bindSoDinhDanhCaNhan = true;

                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindSoDinhDanhCaNhan) {
                    qPos.add(soDinhDanhCaNhan);
                }

                List<DoiTuongTienAn> list = q.list();

                if (list.isEmpty()) {
                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                        finderArgs, list);
                } else {
                    if ((list.size() > 1) && _log.isWarnEnabled()) {
                        _log.warn(
                            "DoiTuongTienAnPersistenceImpl.fetchBySoDinhDanhCaNhan(String, boolean) with parameters (" +
                            StringUtil.merge(finderArgs) +
                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
                    }

                    DoiTuongTienAn doiTuongTienAn = list.get(0);

                    result = doiTuongTienAn;

                    cacheResult(doiTuongTienAn);

                    if ((doiTuongTienAn.getSoDinhDanhCaNhan() == null) ||
                            !doiTuongTienAn.getSoDinhDanhCaNhan()
                                               .equals(soDinhDanhCaNhan)) {
                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                            finderArgs, doiTuongTienAn);
                    }
                }
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (DoiTuongTienAn) result;
        }
    }

    /**
     * Removes the doi tuong tien an where soDinhDanhCaNhan = &#63; from the database.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the doi tuong tien an that was removed
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn removeBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws NoSuchDoiTuongTienAnException, SystemException {
        DoiTuongTienAn doiTuongTienAn = findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

        return remove(doiTuongTienAn);
    }

    /**
     * Returns the number of doi tuong tien ans where soDinhDanhCaNhan = &#63;.
     *
     * @param soDinhDanhCaNhan the so dinh danh ca nhan
     * @return the number of matching doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countBySoDinhDanhCaNhan(String soDinhDanhCaNhan)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_SODINHDANHCANHAN;

        Object[] finderArgs = new Object[] { soDinhDanhCaNhan };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_DOITUONGTIENAN_WHERE);

            boolean bindSoDinhDanhCaNhan = false;

            if (soDinhDanhCaNhan == null) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_1);
            } else if (soDinhDanhCaNhan.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_3);
            } else {
                bindSoDinhDanhCaNhan = true;

                query.append(_FINDER_COLUMN_SODINHDANHCANHAN_SODINHDANHCANHAN_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindSoDinhDanhCaNhan) {
                    qPos.add(soDinhDanhCaNhan);
                }

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the doi tuong tien an in the entity cache if it is enabled.
     *
     * @param doiTuongTienAn the doi tuong tien an
     */
    @Override
    public void cacheResult(DoiTuongTienAn doiTuongTienAn) {
        EntityCacheUtil.putResult(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnImpl.class, doiTuongTienAn.getPrimaryKey(),
            doiTuongTienAn);

        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
            new Object[] { doiTuongTienAn.getSoDinhDanhCaNhan() },
            doiTuongTienAn);

        doiTuongTienAn.resetOriginalValues();
    }

    /**
     * Caches the doi tuong tien ans in the entity cache if it is enabled.
     *
     * @param doiTuongTienAns the doi tuong tien ans
     */
    @Override
    public void cacheResult(List<DoiTuongTienAn> doiTuongTienAns) {
        for (DoiTuongTienAn doiTuongTienAn : doiTuongTienAns) {
            if (EntityCacheUtil.getResult(
                        DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                        DoiTuongTienAnImpl.class, doiTuongTienAn.getPrimaryKey()) == null) {
                cacheResult(doiTuongTienAn);
            } else {
                doiTuongTienAn.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all doi tuong tien ans.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(DoiTuongTienAnImpl.class.getName());
        }

        EntityCacheUtil.clearCache(DoiTuongTienAnImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the doi tuong tien an.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(DoiTuongTienAn doiTuongTienAn) {
        EntityCacheUtil.removeResult(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnImpl.class, doiTuongTienAn.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache(doiTuongTienAn);
    }

    @Override
    public void clearCache(List<DoiTuongTienAn> doiTuongTienAns) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (DoiTuongTienAn doiTuongTienAn : doiTuongTienAns) {
            EntityCacheUtil.removeResult(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                DoiTuongTienAnImpl.class, doiTuongTienAn.getPrimaryKey());

            clearUniqueFindersCache(doiTuongTienAn);
        }
    }

    protected void cacheUniqueFindersCache(DoiTuongTienAn doiTuongTienAn) {
        if (doiTuongTienAn.isNew()) {
            Object[] args = new Object[] { doiTuongTienAn.getSoDinhDanhCaNhan() };

            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                args, Long.valueOf(1));
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                args, doiTuongTienAn);
        } else {
            DoiTuongTienAnModelImpl doiTuongTienAnModelImpl = (DoiTuongTienAnModelImpl) doiTuongTienAn;

            if ((doiTuongTienAnModelImpl.getColumnBitmask() &
                    FINDER_PATH_FETCH_BY_SODINHDANHCANHAN.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        doiTuongTienAn.getSoDinhDanhCaNhan()
                    };

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                    args, Long.valueOf(1));
                FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                    args, doiTuongTienAn);
            }
        }
    }

    protected void clearUniqueFindersCache(DoiTuongTienAn doiTuongTienAn) {
        DoiTuongTienAnModelImpl doiTuongTienAnModelImpl = (DoiTuongTienAnModelImpl) doiTuongTienAn;

        Object[] args = new Object[] { doiTuongTienAn.getSoDinhDanhCaNhan() };

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN, args);
        FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN, args);

        if ((doiTuongTienAnModelImpl.getColumnBitmask() &
                FINDER_PATH_FETCH_BY_SODINHDANHCANHAN.getColumnBitmask()) != 0) {
            args = new Object[] {
                    doiTuongTienAnModelImpl.getOriginalSoDinhDanhCaNhan()
                };

            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SODINHDANHCANHAN,
                args);
            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_SODINHDANHCANHAN,
                args);
        }
    }

    /**
     * Creates a new doi tuong tien an with the primary key. Does not add the doi tuong tien an to the database.
     *
     * @param doiTuongTienAnId the primary key for the new doi tuong tien an
     * @return the new doi tuong tien an
     */
    @Override
    public DoiTuongTienAn create(long doiTuongTienAnId) {
        DoiTuongTienAn doiTuongTienAn = new DoiTuongTienAnImpl();

        doiTuongTienAn.setNew(true);
        doiTuongTienAn.setPrimaryKey(doiTuongTienAnId);

        return doiTuongTienAn;
    }

    /**
     * Removes the doi tuong tien an with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param doiTuongTienAnId the primary key of the doi tuong tien an
     * @return the doi tuong tien an that was removed
     * @throws qldc.pm.tienan.NoSuchDoiTuongTienAnException if a doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn remove(long doiTuongTienAnId)
        throws NoSuchDoiTuongTienAnException, SystemException {
        return remove((Serializable) doiTuongTienAnId);
    }

    /**
     * Removes the doi tuong tien an with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the doi tuong tien an
     * @return the doi tuong tien an that was removed
     * @throws qldc.pm.tienan.NoSuchDoiTuongTienAnException if a doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn remove(Serializable primaryKey)
        throws NoSuchDoiTuongTienAnException, SystemException {
        Session session = null;

        try {
            session = openSession();

            DoiTuongTienAn doiTuongTienAn = (DoiTuongTienAn) session.get(DoiTuongTienAnImpl.class,
                    primaryKey);

            if (doiTuongTienAn == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchDoiTuongTienAnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(doiTuongTienAn);
        } catch (NoSuchDoiTuongTienAnException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected DoiTuongTienAn removeImpl(DoiTuongTienAn doiTuongTienAn)
        throws SystemException {
        doiTuongTienAn = toUnwrappedModel(doiTuongTienAn);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(doiTuongTienAn)) {
                doiTuongTienAn = (DoiTuongTienAn) session.get(DoiTuongTienAnImpl.class,
                        doiTuongTienAn.getPrimaryKeyObj());
            }

            if (doiTuongTienAn != null) {
                session.delete(doiTuongTienAn);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (doiTuongTienAn != null) {
            clearCache(doiTuongTienAn);
        }

        return doiTuongTienAn;
    }

    @Override
    public DoiTuongTienAn updateImpl(
        qldc.pm.tienan.model.DoiTuongTienAn doiTuongTienAn)
        throws SystemException {
        doiTuongTienAn = toUnwrappedModel(doiTuongTienAn);

        boolean isNew = doiTuongTienAn.isNew();

        Session session = null;

        try {
            session = openSession();

            if (doiTuongTienAn.isNew()) {
                session.save(doiTuongTienAn);

                doiTuongTienAn.setNew(false);
            } else {
                session.merge(doiTuongTienAn);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !DoiTuongTienAnModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }

        EntityCacheUtil.putResult(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
            DoiTuongTienAnImpl.class, doiTuongTienAn.getPrimaryKey(),
            doiTuongTienAn);

        clearUniqueFindersCache(doiTuongTienAn);
        cacheUniqueFindersCache(doiTuongTienAn);

        return doiTuongTienAn;
    }

    protected DoiTuongTienAn toUnwrappedModel(DoiTuongTienAn doiTuongTienAn) {
        if (doiTuongTienAn instanceof DoiTuongTienAnImpl) {
            return doiTuongTienAn;
        }

        DoiTuongTienAnImpl doiTuongTienAnImpl = new DoiTuongTienAnImpl();

        doiTuongTienAnImpl.setNew(doiTuongTienAn.isNew());
        doiTuongTienAnImpl.setPrimaryKey(doiTuongTienAn.getPrimaryKey());

        doiTuongTienAnImpl.setDoiTuongTienAnId(doiTuongTienAn.getDoiTuongTienAnId());
        doiTuongTienAnImpl.setUserId(doiTuongTienAn.getUserId());
        doiTuongTienAnImpl.setUserName(doiTuongTienAn.getUserName());
        doiTuongTienAnImpl.setCreateDate(doiTuongTienAn.getCreateDate());
        doiTuongTienAnImpl.setModifiedDate(doiTuongTienAn.getModifiedDate());
        doiTuongTienAnImpl.setSoDinhDanhCaNhan(doiTuongTienAn.getSoDinhDanhCaNhan());
        doiTuongTienAnImpl.setXoaAnTich(doiTuongTienAn.getXoaAnTich());

        return doiTuongTienAnImpl;
    }

    /**
     * Returns the doi tuong tien an with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the doi tuong tien an
     * @return the doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchDoiTuongTienAnException if a doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn findByPrimaryKey(Serializable primaryKey)
        throws NoSuchDoiTuongTienAnException, SystemException {
        DoiTuongTienAn doiTuongTienAn = fetchByPrimaryKey(primaryKey);

        if (doiTuongTienAn == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchDoiTuongTienAnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return doiTuongTienAn;
    }

    /**
     * Returns the doi tuong tien an with the primary key or throws a {@link qldc.pm.tienan.NoSuchDoiTuongTienAnException} if it could not be found.
     *
     * @param doiTuongTienAnId the primary key of the doi tuong tien an
     * @return the doi tuong tien an
     * @throws qldc.pm.tienan.NoSuchDoiTuongTienAnException if a doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn findByPrimaryKey(long doiTuongTienAnId)
        throws NoSuchDoiTuongTienAnException, SystemException {
        return findByPrimaryKey((Serializable) doiTuongTienAnId);
    }

    /**
     * Returns the doi tuong tien an with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the doi tuong tien an
     * @return the doi tuong tien an, or <code>null</code> if a doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        DoiTuongTienAn doiTuongTienAn = (DoiTuongTienAn) EntityCacheUtil.getResult(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                DoiTuongTienAnImpl.class, primaryKey);

        if (doiTuongTienAn == _nullDoiTuongTienAn) {
            return null;
        }

        if (doiTuongTienAn == null) {
            Session session = null;

            try {
                session = openSession();

                doiTuongTienAn = (DoiTuongTienAn) session.get(DoiTuongTienAnImpl.class,
                        primaryKey);

                if (doiTuongTienAn != null) {
                    cacheResult(doiTuongTienAn);
                } else {
                    EntityCacheUtil.putResult(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                        DoiTuongTienAnImpl.class, primaryKey,
                        _nullDoiTuongTienAn);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(DoiTuongTienAnModelImpl.ENTITY_CACHE_ENABLED,
                    DoiTuongTienAnImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return doiTuongTienAn;
    }

    /**
     * Returns the doi tuong tien an with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param doiTuongTienAnId the primary key of the doi tuong tien an
     * @return the doi tuong tien an, or <code>null</code> if a doi tuong tien an with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DoiTuongTienAn fetchByPrimaryKey(long doiTuongTienAnId)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) doiTuongTienAnId);
    }

    /**
     * Returns all the doi tuong tien ans.
     *
     * @return the doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DoiTuongTienAn> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the doi tuong tien ans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.DoiTuongTienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of doi tuong tien ans
     * @param end the upper bound of the range of doi tuong tien ans (not inclusive)
     * @return the range of doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DoiTuongTienAn> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the doi tuong tien ans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.DoiTuongTienAnModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of doi tuong tien ans
     * @param end the upper bound of the range of doi tuong tien ans (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DoiTuongTienAn> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<DoiTuongTienAn> list = (List<DoiTuongTienAn>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_DOITUONGTIENAN);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_DOITUONGTIENAN;

                if (pagination) {
                    sql = sql.concat(DoiTuongTienAnModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<DoiTuongTienAn>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<DoiTuongTienAn>(list);
                } else {
                    list = (List<DoiTuongTienAn>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the doi tuong tien ans from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (DoiTuongTienAn doiTuongTienAn : findAll()) {
            remove(doiTuongTienAn);
        }
    }

    /**
     * Returns the number of doi tuong tien ans.
     *
     * @return the number of doi tuong tien ans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_DOITUONGTIENAN);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the doi tuong tien an persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.tienan.model.DoiTuongTienAn")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<DoiTuongTienAn>> listenersList = new ArrayList<ModelListener<DoiTuongTienAn>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<DoiTuongTienAn>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(DoiTuongTienAnImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
